import java.sql.*;

public class DBUtils {
    /**
     * 该方法的作用是链接数据库
     * @return  返回值是创建的连接
     */
    public static Connection getConnection(){
        Connection  con = null;

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (Exception e) {
            System.out.println("驱动类未找到");
        }
        String DBConn = "jdbc:mysql://127.0.0.1:3306/Java?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8";
        //myDB为数据库名
        try {
            con= DriverManager.getConnection(DBConn,"root","root");
        } catch (SQLException e) {
            System.out.println("数据库连接项错误" + e.getMessage());
        }

        return con;
    }

    /**
     * 关闭数据库连接
     */

    public static void close(ResultSet rs, Statement pst, Connection conn){
        if(rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(pst != null){
            try {
                pst.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

}
